#include "headfile.h"
#include "modbus.h"

int16_t instan_flag=0;
int16_t total_flag=0;

uint8_t fluid_flag;
extern int address;  // EEPROM地址
float instant_flow[8] = {0};  // 瞬时流量
long int total_flow[8] = {0};  // 累计流量

extern uint8_t CRC_arry[6];  // CRC校验数组
extern struct buffer send_buff;  // 发送缓冲区
extern struct buffer uart_buff;  // 接收缓冲区
uint8_t analyze_flag = STOP;
uint8_t master_status = SEND;  // 10-发送状态 20-接收状态

extern TFT_eSPI tft;

// 单位
union trans_int16
{
    int16_t data;
    uint8_t arry[2];
}fluid_unit;

// 累计流量
union trans_int32
{
    int32_t data;
    uint8_t arry[4];
}accumulate;

// 瞬时流量
union trans_float
{
    float data;
    uint8_t arry[4];
}speed;

// CRC发送,接收
union trans_uint16
{
    uint16_t data;
    uint8_t arry[2];
}crc_send_num, crc_get_num;

void ask_for_data()
{
    static uint8_t current_device = 0x01;
    static uint8_t instant_or_accumulate = 0;
    CRC_arry[0] = current_device;
    CRC_arry[1] = GET_SPEED;
    if(instant_or_accumulate==0)  // 查瞬时流量
    {
        fluid_flag = current_device*2 + instant_or_accumulate;
        CRC_arry[2] = ADDR_HIGH;
        CRC_arry[3] = ADDR_LOW;
    }
    else  // 查累积流量
    {
        fluid_flag = current_device*2 + instant_or_accumulate;
        CRC_arry[2] = ADDR_TOTAL_HIGH;
        CRC_arry[3] = ADDR_TOTAL_LOW;
    }   
    CRC_arry[4] = LENGTH_HIGH;
    CRC_arry[5] = LENGTH_LOW;
    crc_send_num.data = CRC16(CRC_arry,6);
    Serial2.write(CRC_arry[0]);
    Serial2.write(CRC_arry[1]);
    Serial2.write(CRC_arry[2]);
    Serial2.write(CRC_arry[3]);
    Serial2.write(CRC_arry[4]);
    Serial2.write(CRC_arry[5]);
    Serial2.write(crc_send_num.arry[0]);
    Serial2.write(crc_send_num.arry[1]);
    if(instant_or_accumulate==0)
    {
        instant_or_accumulate++;
    }
    else
    {
        instant_or_accumulate = 0;
        current_device = ++current_device>8 ? 0x01 : current_device;
    }
    delayMicroseconds(2000);
    digitalWrite(18, LOW);   
}

void ask_for_unit()
{
    CRC_arry[0] = fluid_flag / 2;
    CRC_arry[1] = GET_SPEED;
    if(fluid_flag%2==0)  // 瞬时流量单位
    {
        CRC_arry[2] = 0x10;
        CRC_arry[3] = 0x20;
    }
    else  // 累计流量单位
    {
        CRC_arry[2] = 0x10;
        CRC_arry[3] = 0x21;
    }
    CRC_arry[4] = 0x00;
    CRC_arry[5] = 0x02;
    crc_send_num.data = CRC16(CRC_arry,6);
    Serial2.write(CRC_arry[0]);
    Serial2.write(CRC_arry[1]);
    Serial2.write(CRC_arry[2]);
    Serial2.write(CRC_arry[3]);
    Serial2.write(CRC_arry[4]);
    Serial2.write(CRC_arry[5]);
    Serial2.write(crc_send_num.arry[0]);
    Serial2.write(crc_send_num.arry[1]);
    delayMicroseconds(2000);
    digitalWrite(18, LOW);  
}

void analysis()
{
    static uint8_t times=0;
    uint8_t temp_arry[9];
    uint8_t temp_unit_arry[7];
    if(analyze_flag==RUN && get_buffer_len(&uart_buff)==9)  // 读流量
    {
        read_several_buffer(&uart_buff,temp_arry,9);
        crc_get_num.data = CRC16(temp_arry,7);
        if(crc_get_num.arry[0] == temp_arry[7] && crc_get_num.arry[1] == temp_arry[8])
        {
            if((fluid_flag%2)==0)  // 查瞬时流量
            {
                speed.arry[3] = temp_arry[3];
                speed.arry[2] = temp_arry[4];
                speed.arry[1] = temp_arry[5];
                speed.arry[0] = temp_arry[6];
                switch(temp_arry[0])
                {
                    case 0x01: instant_flow[0] = speed.data;break;
                    case 0x02: instant_flow[1] = speed.data;break;
                    case 0x03: instant_flow[2] = speed.data;break;
                    case 0x04: instant_flow[3] = speed.data;break;
                    case 0x05: instant_flow[4] = speed.data;break;
                    case 0x06: instant_flow[5] = speed.data;break;
                    case 0x07: instant_flow[6] = speed.data;break;
                    case 0x08: instant_flow[7] = speed.data;break;
                }
            }
            else
            {
                accumulate.arry[3] = temp_arry[3];
                accumulate.arry[2] = temp_arry[2];
                accumulate.arry[1] = temp_arry[1];
                accumulate.arry[0] = temp_arry[0];
                switch(temp_arry[0])
                {
                    case 0x01: total_flow[0] = accumulate.data;break;
                    case 0x02: total_flow[1] = accumulate.data;break;
                    case 0x03: total_flow[2] = accumulate.data;break;
                    case 0x04: total_flow[3] = accumulate.data;break;
                    case 0x05: total_flow[4] = accumulate.data;break;
                    case 0x06: total_flow[5] = accumulate.data;break;
                    case 0x07: total_flow[6] = accumulate.data;break;
                    case 0x08: total_flow[7] = accumulate.data;break;
                }
            }
        }
    }
    else if(analyze_flag==RUN && get_buffer_len(&uart_buff)==7)  // 读单位
    {
        read_several_buffer(&uart_buff,temp_unit_arry,7);
        crc_get_num.data = CRC16(temp_unit_arry,5);
        if(crc_get_num.arry[0] == temp_unit_arry[5] && crc_get_num.arry[1] == temp_unit_arry[6])
        {
            fluid_unit.arry[1] = temp_unit_arry[3];
            fluid_unit.arry[0] = temp_unit_arry[4];
            if((fluid_flag%2)==0)  // 查瞬时流量
            {
                instan_flag = fluid_unit.data;
            }
            else
            {
                total_flag = fluid_unit.data;
            }
        }
    }
    else if(analyze_flag==ERROR)  // 报错显示
    {
        tft.setCursor(10, 115, 1);
        tft.print("ERROR:");
        tft.drawNumber(fluid_flag/2, 80, 115, 1); 
    }
    if(times<16)
        times++;
    else  // 掉电保护
    {
        for(uint8_t i=0;i<8;i++)
        {
            EEPROM.writeLong(address, total_flow[i]);
            address += sizeof(long);
        }
        EEPROM.commit();
        address = 0;
        times = 0;
    }
    analyze_flag = STOP;
    delayMicroseconds(2000);
    digitalWrite(18, HIGH);  
}